//
//  ConfigureDataAction.m
//  HdStore
//
//  Created by zhenqiu on 16/4/28.
//  Copyright © 2016年 zhenqiu. All rights reserved.
//

#import "ConfigureDataAction.h"
//#import "GetJsonData.h"
//#import "ClientApi.h"
//#import "NotificationManager.h"

static  NSDictionary * getChannelFileDic(){
  
  static NSDictionary * dic = nil;
  dic = @{@"0":@"loan_info1",@"1":@"loan_id_staff",@"2":@"loan_id_trader",@"3":@"loan_id_eboss",@"4":@"loan_id_freedom",@"5":@"loan_id_boss"};
  return dic;
}

static  NSDictionary * getChannelFileDic_Baoxian(){
  
  static NSDictionary * dic = nil;
  dic = @{@"0":@"loan_info1",@"1":@"loan_id_staff_baoxian",@"2":@"loan_id_trader_baoxian",@"3":@"loan_id_eboss_baoxian",@"4":@"loan_id_freedom_baoxian",@"5":@"loan_id_boss_baoxian"};
  return dic;
}

@implementation ConfigureDataAction

@synthesize baseInputItemsDic;
@synthesize extendInputItemsDic;
@synthesize channelDataArray;
@synthesize channelId;
@synthesize pageCount;
@synthesize pageIndex;


-(instancetype) init{
  
  if(self = [super init]){
    
    baseInputItemsDic = nil;
    extendInputItemsDic = nil;
    channelDataArray = nil;
    channelId = -1;
    pageCount = -1;
    pageIndex = -1;
  }
  
  return self;
  
}


-(void) start {
  NSDictionary *dic = nil;
  NotificationManager *manager = [NotificationManager getInstance];
  NSInteger status = manager.loan_baoxian_status;
  if (status) {
    dic = getChannelFileDic();

  }else{
    dic = getChannelFileDic_Baoxian();
  }
  
  NSString * fileName = [dic valueForKey:[NSString stringWithFormat:@"%ld",(long)channelId]];
  
  baseInputItemsDic = [[GetJsonData standardGetInstance] getJsonBaseData:@"base" withKey:@"data"];
  extendInputItemsDic = [[GetJsonData standardGetInstance] getJsonBaseData:fileName withKey:@"data"];
  channelDataArray = [self genChannelDataArray:fileName];
  
}







#pragma mark -
#pragma mark channel configure data

-(id)genChannelDataArray:(NSString*)fileName{
  
  
  NSArray * dArray = [[GetJsonData standardGetInstance]getJsonBaseData:fileName withKey:@"configure"];
  
  id pageData = [[GetJsonData standardGetInstance] getJsonBaseData:fileName withKey:@"pages"];
  if(pageData){
    
    pageCount = [pageData count];
    
    if(pageIndex > 0){
      
      NSString * key = [pageData objectAtIndex:pageIndex];
      
      NSArray * array = [[GetJsonData standardGetInstance] getJsonBaseData:fileName withKey:key];
      return array;
    }
  }
  
  return dArray;
}


#pragma mark
#pragma mark gen item data from configure

-(id)getItemDic:(NSString*)key{
  
  
  if([extendInputItemsDic valueForKey:key]){
    
    return [extendInputItemsDic valueForKey:key];
  }
  
  if([baseInputItemsDic valueForKey:key]){
    
    return [baseInputItemsDic valueForKey:key];
    
  }
  
  return nil;
  
}


-(id)genOriginalItemsWithDic:(id)dic{ // {"0":["name","id"],"1":[]...}
  
  NSMutableDictionary *subDic = [NSMutableDictionary dictionaryWithCapacity:3];
  
  for (NSInteger j = 0; j < [[dic allKeys] count]; j++) {
    
    NSString *key = [[dic allKeys] objectAtIndex:j];
    NSArray *subsubs = [dic valueForKey:key];
    
    NSMutableArray * itemArray = [[NSMutableArray alloc] initWithCapacity:5];
    for (NSInteger k = 0; k < [subsubs count]; k++) {
      
      id itemDic = [self getItemDic:[subsubs objectAtIndex:k]];
      DataModle *itemData = [ItemDataFactory createWithDic:itemDic];
      if(itemData){
        
        [itemArray addObject:itemData];
      }
      
    }
    
    [subDic setValue:itemArray forKey:key];
    
  }
  
  return subDic;
  
}

-(id)genOriginalItemsWithArray:(id)array{ // ["name","id",""]
  
  NSMutableArray * itemArray = [[NSMutableArray alloc] initWithCapacity:5];
  for (NSInteger j = 0; j < [array count]; j++) {
    
    NSString* key = [array objectAtIndex:j];
    id itemDic = [self getItemDic:key];
    DataModle * itemData = [ItemDataFactory createWithDic:itemDic];
    
    if(itemData){
      
      [itemArray addObject:itemData];
    }
    
  }
  
  return itemArray;
}


-(id) genOriginalItems{
  
  return  [self genOriginalItemsWithRecurArray:channelDataArray];
}


-(id)genOriginalItemsWithRecurArray:(id)inArray{
  
  NSMutableArray *originalArray  = [NSMutableArray arrayWithCapacity:15];
  NSInteger count = [inArray count];
  for (NSInteger i = 0; i < count; i++) {
    
    NSDictionary * dic = [inArray objectAtIndex:i];
    NSString * title = [dic valueForKey:@"title"];
    NSString * key = [dic valueForKey:@"key"];
    id subs = [dic valueForKey:@"subs"];
    
    // section dic
    NSMutableDictionary * fdic = [NSMutableDictionary dictionaryWithCapacity:4];
    [fdic setValue:title forKey:@"title"];
    [fdic setValue:key forKey:@"key"];
    
    if ([subs isKindOfClass:[NSDictionary class]]) { // dic
      
      id subDic = [self genOriginalItemsWithDic:subs];
      [fdic setValue:subDic forKey:@"subs"];
      
    }else if([[subs objectAtIndex:0] isKindOfClass:[NSDictionary class]]){ // array (item is dic)
      
      id dicArray = [self genOriginalItemsWithRecurArray:subs];
      [fdic setValue:dicArray forKey:@"subs"];
      
    }else { // array (item is string)
      
      id itemArray = [self genOriginalItemsWithArray:subs];
      [fdic setValue:itemArray forKey:@"subs"];
    }
    
    
    [originalArray addObject:fdic];
    
  }
  
  return originalArray;
  
}

// mashang need optim

-(id)genDataItems:(NSString*)key withType:(NSInteger) type{
  
  NSMutableArray * dataArray = [NSMutableArray arrayWithCapacity:4];
  NSArray * originalArray = [self genOriginalItems];
  for (NSInteger i = 0; i < [originalArray count]; i ++) {
    
    NSDictionary * dic = [originalArray objectAtIndex:i];
    NSString * lkey = [dic valueForKey:@"key"];
    
    if([key isEqualToString:lkey]){  // dic
      
      NSDictionary * subdic = [dic valueForKey:@"subs"];
      NSString * typeStr = [NSString stringWithFormat:@"%ld",(long)type];
      NSArray * subsubs = [subdic valueForKey:typeStr];
      // create new dic
      NSMutableDictionary * newDic = [NSMutableDictionary dictionaryWithCapacity:3];
      [newDic setValue:[dic valueForKey:@"key"] forKey:@"key"];
      [newDic setValue:[dic valueForKey:@"title"] forKey:@"title"];
      [newDic setValue:subsubs forKey:@"subs"];
      
      [dataArray addObject:newDic];
      
    }else{// array
      
      [dataArray addObject:dic];
      
    }
  }
  
  return dataArray;
  
}

-(id)genDataitems:(NSDictionary*)typeDic{
  
  NSMutableArray * dataArray = [NSMutableArray arrayWithCapacity:4];
  NSArray * originalArray = [self genOriginalItems];
  for (NSInteger i = 0; i < [originalArray count]; i ++) {
    
    NSDictionary * dic = [originalArray objectAtIndex:i];
    NSString * lkey = [dic valueForKey:@"key"];
    
    if([[typeDic allKeys] containsObject:lkey]){  // dic
      
      NSInteger type = [[typeDic valueForKey:lkey] integerValue];
      
      NSDictionary * subdic = [dic valueForKey:@"subs"];
      NSString * typeStr = [NSString stringWithFormat:@"%ld",(long)type];
      NSArray * subsubs = [subdic valueForKey:typeStr];
      // create new dic
      NSMutableDictionary * newDic = [NSMutableDictionary dictionaryWithCapacity:3];
      [newDic setValue:[dic valueForKey:@"key"] forKey:@"key"];
      [newDic setValue:[dic valueForKey:@"title"] forKey:@"title"];
      [newDic setValue:subsubs forKey:@"subs"];
      
      [dataArray addObject:newDic];
      
    }else{// array
      
      [dataArray addObject:dic];
      
    }
  }
  
  return dataArray;
  
  
}


@end
